﻿// -----------------------------------------------------------------------
// <copyright file="QueueLogic.cs" company="Abyssalsoft">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace Metal.Player.Playlist
{
    using System;
    using System.Collections.Generic;
    using System.Text;

    /// <summary>
    /// Contains the methods for Queue form. To bind this methods on your buttons, specify in your button/menu (in xml theme file): "command="MethodName"
    /// </summary>
    public class QueueLogic
    {
        #region Members

        /// <summary> Stores reference to our instance </summary>
        private static QueueLogic instance = null;

        #endregion

        #region Constructor

        /// <summary> Gets the current instance of this class (Singleton - Thread safety) </summary>
        public static QueueLogic Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new QueueLogic();
                }

                return instance;
            }
        }

        #endregion

        #region Methods called by reflection

        /// <summary>
        /// Play selected file
        /// </summary>
        public void PlaySelected()
        {
            try
            {
                if (QueueController.View.ListSelectedRow != null)
                {
                    Core.Player.Playback.Instance.Open(QueueController.View.ListSelectedRow.Cells[1].Value.ToString());
                    Core.Player.Playback.Instance.Play();
                }
            }
            catch (Exception ex)
            {
                Metal.User.UserMessage.ShowError(ex);
            }
        }

        /// <summary>
        /// Remove selected from queue
        /// </summary>
        public void RemoveFromQueue()
        {
            try
            {
                if (QueueController.View.ListSelectedRow != null)
                {
                    int selectedIndex = Convert.ToInt32(QueueController.View.ListSelectedRow.Cells[0].RowIndex);
                    QueueController.ListDataSet.Tables[0].Rows[selectedIndex].Delete();
                    QueueController.SaveCurrentPlaylist();
                }
            }
            catch (Exception ex)
            {
                Metal.User.UserMessage.ShowError(ex);
            }
        }

        /// <summary>
        /// Clear queue list
        /// </summary>
        public void RemoveAll()
        {
            try
            {
                QueueController.ListDataSet.Tables[0].Rows.Clear();
                QueueController.SaveCurrentPlaylist();
            }
            catch (Exception ex)
            {
                Metal.User.UserMessage.ShowError(ex);
            }
        }

        #endregion
    }
}
